package cn.com.nxt.txb.httpdelegate;

import android.os.Build;
import android.util.Log;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import cn.com.nxt.txb.HttpProtocol;
import cn.com.nxt.txb.ResponseStatus;
import cn.com.nxt.txb.model.BaseModel;
import cn.com.nxt.txb.util.AppConstant;

/**
 * Created by zhangling on 2017/6/9.
 * 直播相关
 */

public class LiveDelegate {
    private HttpProtocol protocol;

    public LiveDelegate(HttpProtocol protocol) {
        this.protocol = protocol;
    }

    public void livePlay(String title, final byte flag) {
        RequestParams rp = new RequestParams(AppConstant.LIVE_PLAY);
        rp.setAsJsonContent(true);
        rp.addQueryStringParameter("title", title);
        rp.addQueryStringParameter("outType", "flv");
        x.http().get(rp, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                // Gson gson = new Gson();
                Log.v("--livePlay_success--", result);
                BaseModel model = BaseModel.parseResult(result);
                String code = model.getCode();
                if (code.equals("10000"))
                    protocol.success(model.getData(), flag);
                else
                    protocol.failure("请求失败，请重试", flag, ResponseStatus.REQUEST_FAILURE);
            }

            //请求异常后的回调方法
            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                Log.v("--livePlay_error--", ex.getMessage());
                protocol.failure("网络连接失败，请稍后重试", flag, ResponseStatus.NET_ERROR);
            }

            //主动调用取消请求的回调方法
            @Override
            public void onCancelled(CancelledException cex) {
                protocol.cancelled(flag);
            }

            @Override
            public void onFinished() {
                protocol.finished(flag);
            }
        });
    }

    public void liveAddress(final byte flag) {
        RequestParams rp = new RequestParams(AppConstant.LIVE_ADDRESS);
        rp.setAsJsonContent(true);
        x.http().get(rp, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                // Gson gson = new Gson();
                Log.v("--liveAddress_success--", result);
                BaseModel model = BaseModel.parseResult(result);
                String code = model.getCode();
                if (code.equals("10000"))
                    protocol.success(model.getData(), flag);
                else
                    protocol.failure("请求失败，请重试", flag, ResponseStatus.REQUEST_FAILURE);
            }

            //请求异常后的回调方法
            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                Log.v("--liveAddress_error--", ex.getMessage());
                protocol.failure("网络连接失败，请稍后重试", flag, ResponseStatus.NET_ERROR);
            }

            //主动调用取消请求的回调方法
            @Override
            public void onCancelled(CancelledException cex) {
                protocol.cancelled(flag);
            }

            @Override
            public void onFinished() {
                protocol.finished(flag);
            }
        });
    }

    public void livePublish(String title, final byte flag) {
        RequestParams rp = new RequestParams(AppConstant.LIVE_PUBLISH);
        rp.setAsJsonContent(true);
        rp.addQueryStringParameter("title", title);
        x.http().get(rp, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                // Gson gson = new Gson();
                Log.v("--livePublish_success--", result);
                BaseModel model = BaseModel.parseResult(result);
                String code = model.getCode();
                if (code.equals("10000"))
                    protocol.success(model.getData(), flag);
                else
                    protocol.failure("请求失败，请重试", flag, ResponseStatus.REQUEST_FAILURE);
            }

            //请求异常后的回调方法
            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                Log.v("--livePublish_error--", ex.getMessage());
                protocol.failure("网络连接失败，请稍后重试", flag, ResponseStatus.NET_ERROR);
            }

            //主动调用取消请求的回调方法
            @Override
            public void onCancelled(CancelledException cex) {
                protocol.cancelled(flag);
            }

            @Override
            public void onFinished() {
                protocol.finished(flag);
            }
        });
    }

    public void signalAddr(final byte flag) {
        RequestParams rp = new RequestParams(AppConstant.SIGNAL_ADDR);
        rp.setAsJsonContent(true);
        x.http().get(rp, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                // Gson gson = new Gson();
                Log.v("--signalAddr_success--", result);
                BaseModel model = BaseModel.parseResult(result);
                String code = model.getCode();
                if (code.equals("10000"))
                    protocol.success(model.getData(), flag);
                else
                    protocol.failure("请求失败，请重试", flag, ResponseStatus.REQUEST_FAILURE);
            }

            //请求异常后的回调方法
            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                Log.v("--signalAddr_error--", ex.getMessage());
                protocol.failure("网络连接失败，请稍后重试", flag, ResponseStatus.NET_ERROR);
            }

            //主动调用取消请求的回调方法
            @Override
            public void onCancelled(CancelledException cex) {
                protocol.cancelled(flag);
            }

            @Override
            public void onFinished() {
                protocol.finished(flag);
            }
        });
    }

    public void liveStart(String ip,String param,final byte flag) {
        RequestParams rp = new RequestParams(ip+AppConstant.LIVE_START);
        Log.e("--liveStart_add--", ip+AppConstant.LIVE_START);
        Log.e("--liveStart_param--", param);
        if (Build.VERSION.SDK != null && Build.VERSION.SDK_INT > 13) {
            rp.addHeader("Connection", "close");
            //rp.setRequestProperty("Connection", "close");
        }
        //rp.setAsJsonContent(true);
        rp.setBodyContent(param);
        x.http().post(rp, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                // Gson gson = new Gson();
                Log.e("--liveStart_success--", result);
                BaseModel model = BaseModel.parseResult(result);
                String code = model.getCode();
                if (code.equals("10000"))
                    protocol.success(model.getData(), flag);
                else
                    protocol.failure("请求失败，请重试", flag, ResponseStatus.REQUEST_FAILURE);
            }

            //请求异常后的回调方法
            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                Log.e("--liveStart_error--", ex.getMessage());
                protocol.failure("网络连接失败，请稍后重试", flag, ResponseStatus.NET_ERROR);
            }

            //主动调用取消请求的回调方法
            @Override
            public void onCancelled(CancelledException cex) {
                protocol.cancelled(flag);
            }

            @Override
            public void onFinished() {
                protocol.finished(flag);
            }
        });
    }
}
